<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Module_Blockstyle_Property_Background extends JFX_Module_Blockstyle_Property_Base
{
	public $id = 'background';
	public $cssName = 'background';
	public $title = 'Background';
	public $repeatX = true;
	public $repeatY = true;
	public $positionLeft = 0;
	public $positionRight = 0;
	public $instanceId = 'block-background-0';
	
	

	public function init(){

		$backgroundColorWidget = new JFX_Module_Blockstyle_Widget_Color($this);
		$backgroundColorWidget->value = '000000';
		$backgroundColorWidget->name = 'Background Colour';
		$backgroundColorWidget->instanceId = $this->instanceId.'-color';
		$backgroundColorWidget->cssName = 'background-color';
		$this->widgets[] = $backgroundColorWidget;

		$imageWidget = new JFX_Module_Blockstyle_Widget_Image($this);
		$imageWidget->value = '';
		$imageWidget->name = 'Background Image';
		$imageWidget->instanceId = $this->instanceId.'-image';
		$imageWidget->cssName = 'background-image';
		$imageWidget->buildImageList();
		$this->widgets[] = $imageWidget;

		$repeatWidget = new JFX_Module_Blockstyle_Widget_Multi($this);
		$repeatWidget->instanceId = $this->instanceId.'-repeat';
		$repeatWidget->name = 'Image Repeat';
		$repeatWidget->addOption('Repeat-X', 'repeat-x');
		$repeatWidget->addOption('Repeat-Y', 'repeat-y');
		$repeatWidget->addOption('Repeat', 'repeat');
		$repeatWidget->addOption('No Repeat', 'no-repeat');
		$repeatWidget->multiple = false;
		$repeatWidget->cssName = 'background-repeat';
		$repeatWidget->values[] = 'repeat';
		$this->widgets[] = $repeatWidget;

		$leftPositionWidget = new JFX_Module_Blockstyle_Widget_Slider($this);
		$leftPositionWidget->unit = 'px';
		$leftPositionWidget->name = 'Left position';
		$leftPositionWidget->instanceId = $this->instanceId.'-leftpos';
		$leftPositionWidget->cssName = 'background-position-left';
		$leftPositionWidget->units = array(
			'px',
			'%',
			'left',
			'center',
			'right'
		);
		$leftPositionWidget->unitNames = array(
			'Pixels',
			'Percent',
			'Left',
			'Center',
			'Right'
		);
		$this->widgets[] = $leftPositionWidget;

		$topPositionWidget = new JFX_Module_Blockstyle_Widget_Slider($this);
		$topPositionWidget->unit = 'px';
		$topPositionWidget->name = 'Top position';
		$topPositionWidget->instanceId = $this->instanceId.'-toppos';
		$topPositionWidget->cssName = 'background-position-top';
		$topPositionWidget->units = array(
			'px',
			'%',
			'top',
			'center',
			'bottom'
		);
		$topPositionWidget->unitNames = array(
			'Pixels',
			'Percent',
			'Top',
			'Center',
			'Bottom'
		);
		$this->widgets[] = $topPositionWidget;


		$attachmentWidget = new JFX_Module_Blockstyle_Widget_Multi($this);
		$attachmentWidget->unit = 'px';
		$attachmentWidget->name = 'Background Attachment';
		$attachmentWidget->instanceId = $this->instanceId.'-attachment';
		$attachmentWidget->cssName = 'background-attachment';
		$attachmentWidget->addOption('Fixed', 'fixed');
		$attachmentWidget->addOption('Scroll', 'scroll');
		$attachmentWidget->addOption('Inherit', 'inherit');
		$attachmentWidget->value = 'scroll';
		
		$this->widgets[] = $attachmentWidget;




	}

	public function getCssValue(){


	}

	public function __toCss(){
		$outputString = '';

		if(!$this->hasEnabledWidget()){
			return $outputString;
		}

		if(!$this->hasEnabledWidget() || $this->cssName=='' || $this->cssName != $this->id){
			return '/*not enabled '.$this->cssName.'*/ '.$outputString;
		}

		$leftPositionWidget = $this->getWidget($this->instanceId.'-leftpos');
		$topPositionWidget = $this->getWidget($this->instanceId.'-toppos');

		if($leftPositionWidget->enabled && $topPositionWidget->enabled){
			if($leftPositionWidget->unit == 'px' || $leftPositionWidget->unit == '%'){
				$imagePosLeft = $leftPositionWidget->value.$leftPositionWidget->unit;
			}else{
				$imagePosLeft = $leftPositionWidget->unit;
			}
			if($topPositionWidget->unit == 'px' || $topPositionWidget->unit == '%'){
				$imagePosTop = $topPositionWidget->value.$topPositionWidget->unit;
			}else{
				$imagePosTop = $topPositionWidget->unit;
			}
			if(trim($imagePosLeft) && trim($imagePosTop)){
				$outputString .= '	background-position: '.$imagePosLeft.' '.$imagePosTop.';
';
			}
		}



		$imageWidget = $this->getWidget($this->instanceId.'-image');
		$image = $imageWidget->value;

		if($imageWidget->enabled && trim($imageWidget->filename)!=''){
			$outputString .= '	background-image: url(:|imageUrl|:?filepath='.urlencode($imageWidget->path).'&filename='.urlencode($imageWidget->filename).');
';
		}

		$backgroundColorWidget = $this->getWidget($this->instanceId.'-color');

		$backgroundColor = $backgroundColorWidget->value;
		if($backgroundColorWidget->enabled && trim($backgroundColor)!=''){
			$outputString .= '	background-color: #'.$backgroundColor.';
';
		}

		$repeatWidget = $this->getWidget($this->instanceId.'-repeat');

		$backgroundRepeat = $repeatWidget->values[0];
		if($repeatWidget->enabled && trim($backgroundRepeat)!=''){
			$outputString .= '	background-repeat: '.$backgroundRepeat.';
';
		}

		$attachmentWidget = $this->getWidget($this->instanceId.'-attachment');
		$attachmentOutput = $attachmentWidget->value;
		if($attachmentWidget->enabled && trim($attachmentOutput)!=''){
			$outputString .= '	background-attachment: '.$attachmentOutput.';
';
		}


		

		return $outputString;
	}


	public function setPropertyFromJson($k, $v){
		if(!in_array($k, $this->privateProperties)){
			$this->$k = $v;
		}

		try{
		
		}catch(Exception $e){
			JFX::addError('Cant add the '.$k);
			
		}
		
	}

	public function prepareJson(){
		parent::prepareJson();
		$imageWidget = $this->getWidget($this->instanceId.'-image');
		$imageWidget->buildImageList();
	}


}

